﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Resources;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using RestaurantBooker.Helpers;
using RestaurantBooker.Converters;

namespace RestaurantBooker.Resources
{

    public static class StaticResources
    {
        //public static LinearGradientBrush AllocatedRowBackground = Application.Current.Resources["AllocatedRowBackground"] as LinearGradientBrush;
        
        //public static LinearGradientBrush NotAllocatedRowBackground = Application.Current.Resources["NotAllocatedRowBackground"] as LinearGradientBrush;
        
        //public static LinearGradientBrush ChildRowBackground = Application.Current.Resources["ChildRowBackground"] as LinearGradientBrush;
        
        private static Dictionary<string, BitmapImage> _imageCache = new Dictionary<string, BitmapImage>();

        private static BitmapImage GetImageFromCashe(string key)
        {
            // если нет в кеше - загрузим и добавим туда!
            if (!_imageCache.ContainsKey(key))
            {
                var image = ResourceHelper.GetBitmap(key);
                // если рисунок не загружен - лажа - генерим исключение!
                if (image == null)
                    throw new ArgumentException(string.Format("StaticResources.GetImageFromCashe -  Image resource '{0}' not found!", key));
                else
                {
                    // добавим в кеш
                    _imageCache.Add(key, image);

                    return image;
                }
            }
            else // вернём из кеша!
            {
                return _imageCache[key];
            }
        }

        internal static BitmapImage WarningIcon32x32
        {
            get
            {
                return GetImageFromCashe("Resources/Images/warning32x32.png");
            }
        }

        internal static BitmapImage InformationIcon32x32
        {
            get
            {
                return GetImageFromCashe("Resources/Images/Information32x32.png");
            }
        }

        internal static BitmapImage ConfirmationIcon32x32
        {
            get
            {
                return GetImageFromCashe("Resources/Images/Confirmation32x32.png");
            }
        }

        
    }

    public class ResourceHelper
    {

        public static string ExecutingAssemblyName
        {
            get
            {
                string name = System.Reflection.Assembly.GetExecutingAssembly().FullName;
                return name.Substring(0, name.IndexOf(','));
            }
        }

        public static Stream GetStream(string relativeUri, string assemblyName)
        {
            StreamResourceInfo res = Application.GetResourceStream(new Uri(assemblyName + ";component/" + relativeUri, UriKind.Relative));
            
            if (res == null)
            {
                res = Application.GetResourceStream(new Uri(relativeUri, UriKind.Relative));
            }
            if (res != null)
            {
                return res.Stream;
            }
            else
            {
                return null;
            }
        }


        public static Stream GetStream(string relativeUri)
        {
            return GetStream(relativeUri, ExecutingAssemblyName);
        }

        public static BitmapImage GetBitmap(string relativeUri, string assemblyName)
        {
            Stream s = GetStream(relativeUri, assemblyName);
            if (s == null) return null;
            using (s)
            {
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(s);
                return bmp;
            }
        }

        public static BitmapImage GetBitmap(string relativeUri)
        {
            return GetBitmap(relativeUri, ExecutingAssemblyName);
        }

        public static string GetString(string relativeUri, string assemblyName)
        {
            Stream s = GetStream(relativeUri, assemblyName);
            if (s == null) return null;
            using (StreamReader reader = new StreamReader(s))
            {
                return reader.ReadToEnd();
            }
        }

        public static string GetString(string relativeUri)
        {
            return GetString(relativeUri, ExecutingAssemblyName);
        }

        public static FontSource GetFontSource(string relativeUri, string assemblyName)
        {
            Stream s = GetStream(relativeUri, assemblyName);
            if (s == null) return null;
            using (s)
            {
                return new FontSource(s);
            }
        }

        public static FontSource GetFontSource(string relativeUri)
        {
            return GetFontSource(relativeUri, ExecutingAssemblyName);
        }

        public static object GetXamlObject(string relativeUri, string assemblyName)
        {
            string str = GetString(relativeUri, assemblyName);
            if (str == null) return null;
            object obj = System.Windows.Markup.XamlReader.Load(str);
            return obj;
        }

        public static object GetXamlObject(string relativeUri)
        {
            return GetXamlObject(relativeUri, ExecutingAssemblyName);
        }

    }
}
